package com.yuven.appframework.util.imageloader

import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.transition.Transition
import com.yuven.baselib.utils.ViewUtils

/**
@author: subao
@date: 2021/12/27
 */
class GlideImageEngine: IImageLoadEngine {


    override fun loadImage(placeholder: Int, error: Int, url: String?, imageView: ImageView) {
        Glide.with(imageView.context).asBitmap()
            .apply(
                RequestOptions()
                    .placeholder(placeholder)
                    .dontAnimate()
                    .error(error)
                    .centerCrop()
            ).load(url)
            .into(imageView)
    }

    override fun loadImage(placeholder: Int, error: Int, url: String?, view: View) {
        Glide.with(view.context).load(url)
            .apply(
                RequestOptions()
                    .placeholder(placeholder)
                    .error(error)
                    .dontAnimate()
                    .centerCrop()
            )
            .into(object : CustomTarget<Drawable?>() {
                override fun onLoadCleared(placeholder: Drawable?) {

                }
                override fun onLoadFailed(errorDrawable: Drawable?) {
                    super.onLoadFailed(errorDrawable)
                    view.setBackgroundResource(error)
                }

                override fun onResourceReady(
                    resource: Drawable,
                    transition: Transition<in Drawable?>?
                ) {
                    view.background = resource
                }
            })
    }

    override fun loadImage(placeholder: Drawable, error: Drawable, url: String?, imageView: ImageView) {
        Glide.with(imageView.context).asBitmap()
            .load(url)
            .apply(
                RequestOptions()
                    .placeholder(placeholder)
                    .dontAnimate()
                    .error(error)
                    .centerCrop()
            ).into(imageView)
    }

    override fun loadImage(
        url: String?,
        imageView: ImageView,
        listener: RequestListener<*>?
    ) {
        Glide.with(imageView.context).load(url)
            .apply(
                RequestOptions()
                    .dontAnimate()
                    .centerCrop()
            )
            .listener(listener as RequestListener<Drawable>?)
            .into(imageView)
    }

    override fun loadImage(url: String?, view: View) {
      loadImage(0,0,url,view)
    }

    override fun loadRoundedImage(url: Any, imageView: ImageView, roundingRadius: Int) {
      loadRoundedImage(0,0, url,imageView,roundingRadius)
    }

    override fun loadRoundedImage(placeholder: Int, error: Int, url: Any, imageView: ImageView, roundingRadius: Int) {
        Glide.with(imageView.context).load(url)
            .apply(
                RequestOptions()
                    .placeholder(placeholder)
                    .error(error)
                    .transform(
                        CenterCrop(), RoundedCorners(ViewUtils.dip2px(roundingRadius.toFloat()))
                    )
            ).into(imageView)
    }

    override fun loadCircleImage(url: Any, imageView: ImageView, placeholder: Int, error: Int) {
        Glide.with(imageView.context).load(url)
            .apply(
                RequestOptions()
                    .placeholder(placeholder)
                    .error(error)
                    .centerCrop()
                    .circleCrop()
            ).into(imageView)
    }
}